<template>
  <div>
    <el-form size="small" :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px">
      <el-form-item label="名称" prop="name">
        <el-input v-model="ruleForm.name" clearable></el-input>
      </el-form-item>
      <el-form-item label="专辑" prop="albumId">
        <el-select v-model="ruleForm.albumId" placeholder="请选择" clearable>
          <el-option v-for="(item, index) of albumList" :key="index" :value="item.id" :label="item.name"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="长度(秒)" prop="timeLength">
          <el-input v-model="ruleForm.timeLength" onkeyup="value=value.replace(/[^\d]/g,'')" clearable></el-input>
      </el-form-item>
      <el-form-item label="文件路径" prop="filePath">
        <el-input v-model="ruleForm.filePath" clearable></el-input>
      </el-form-item>
    <el-form-item label="文件大小" prop="fileSize">
        <el-input v-model="ruleForm.fileSize" onkeyup="value=value.replace(/[^\d]/g,'')" clearable></el-input>
    </el-form-item>
        <el-form-item label="简介" prop="description">
          <el-input type="textarea" v-model="ruleForm.description" clearable></el-input>
        </el-form-item>
    </el-form>
    <div style="text-align: center;">
      <el-button @click="$emit('changeEditMusicFlag', false)">取 消</el-button>
      <el-button type="primary" @click="toSubmit">确 定</el-button>
    </div>
  </div>
</template>

<script>
import * as albumApi from "@/api/albumApi";
import * as musicApi from "@/api/musicApi";
export default {
  data() {
    return {
      ruleForm: {},
      rules: {
        name: [{ required: true, message: "请输入名称", trigger: "blur" }],
        albumId: [{ required: true, message: "请选择专辑", trigger: "blur" }],
        filePath: [{ required: true, message: "请输入文件路径", trigger: "blur" }],
        timeLength: [{ required: true, message: "请输入长度", trigger: "blur" }],
      },
      albumList: [],
    };
  },
  props: {
    editMusicFlag: Boolean,
    editId: Number
  },
  async mounted() {
    await this.getAlbumList();
    this.getMusicInfo();
  },
  watch: {
    async editMusicFlag(value) {
      if (value) {
        await this.getAlbumList();
        this.getMusicInfo();
      }
    }
  },
  methods: {
    getAlbumList() {
      albumApi.getAllAlbum().then(res => {
        if (res.errorCode == 0) {
          this.albumList = res.data
        }
      });
    },
    getMusicInfo() {
      return new Promise((resolve, reject) => {
        musicApi.getMusicInfo({ id: this.editId }).then(res => {
          if (res.errorCode == 0) {
            this.ruleForm = res.data;
          }
        });
      });
    },
    toSubmit() {
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          musicApi.editMusic(this.ruleForm).then(res => {
            if (res.errorCode == 0) {
              this.$message({
                showClose: true,
                message: "编辑成功！",
                type: "success"
              });
              this.$emit("changeEditMusicFlag", false);
            }
          });
        }
      });
    }
  }
};
</script>

<style lang="less" scope>
</style>
